package nexd.com.employ.overly;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.nexd.map.rendering.core.SVGMapBaseOverlay;
import cn.nexd.map.rendering.core.componet.SVGMapView;
import nexd.com.employ.model.LocationModel;

/**
 * POI搜索图层
 * Created by lawrence on 2015/10/14.
 */
public class MorePointOverly extends SVGMapBaseOverlay {

    private static final String TAG = "MorePointOverly";
//    private List<PointF> positions;

    private Map<String, PointF> positions;
    private Context context;
    private int res;

    private float defaultLocationCircleRadius;

    private SVGMapView svgMapView;

    private List<LocationModel> locationModels;

    private String[] colors = new String[]{"#669966", "#FD5B78", "#8A3324", "#EEEEFF", "#B87333", "#DEA681", "#00755E",
            "#C7FFEC", "#C3BED4", "#DDF0ED", "#D9D6CF", "#426AB3", "#FDB933", "#563624", "#171916"};
    //
//    // 为设备Imei号绑定颜色值.
    private Map<String, LocationModel> positionColor;


    public MorePointOverly(Context context, SVGMapView svgMapView) {
        this.context = context;
        this.svgMapView = svgMapView;
        positionColor = new HashMap<>();
        defaultLocationCircleRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, svgMapView.getResources().getDisplayMetrics());
        locationModels = new ArrayList<>();
        this.showLevel = LOCATION_LEVEL - 1;
    }

    public void setRes(int res) {
        this.res = res;
    }


    public void setPositions(Map<String, PointF> positions) {
        if (this.positions == null) {
            this.positions = new HashMap<>();
        } else {
            this.positions.clear();
        }


        this.positions.putAll(positions);

        // TODO: 16/9/26  edit by lawrence 下面两行注释,修改为默认单一颜色,颜色列表中第一个颜色为同意样色
        for (Map.Entry<String, PointF> entry : positions.entrySet()) {
            if (!positionColor.containsKey(entry.getKey())) {
                LocationModel locationModel = new LocationModel();
                locationModel.setMachineImei(entry.getKey());
//                locationModel.setColor(colors[positionColor.size()]);
                locationModel.setColor(colors[0]);
                locationModel.setLocation(entry.getValue());
                Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
                paint.setAntiAlias(true);
                paint.setStyle(Paint.Style.FILL);
//                paint.setColor(Color.parseColor(colors[positionColor.size()]));
                paint.setColor(Color.parseColor(colors[0]));
                locationModel.setLocalPaint(paint);
                positionColor.put(entry.getKey(), locationModel);
            } else {
                positionColor.get(entry.getKey()).setLocation(entry.getValue());
            }
        }
    }

//    public void setPositions(List<LocationModel> locationModels) {
//        this.locationModels.clear();
//        this.locationModels.addAll(locationModels);
//    }

    public void setPositions(List<PointF> positions) {
        if (this.positions != null && this.positions.size() > 0) {
            this.positions.clear();
        }
        if (this.positions != null) {
//            this.positions.addAll(positions);
        }
    }


    @Override
    public void onDestroy() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onTap(MotionEvent motionEvent) {

    }

    @Override
    public void draw(Canvas canvas, Matrix matrix, float v, float v1) {

        Log.d(TAG, "draw: 执行到了这里");
        canvas.save();
        if (this.isVisible && this.positions != null) {

//            for (LocationModel locationModel : locationModels) {
//                float[] target = {locationModel.getLocation().x - svgMapView.getFloorFeature().getOffset_x(), locationModel.getLocation().y - svgMapView.getFloorFeature().getOffset_y()};
//                matrix.mapPoints(target);
//                canvas.drawCircle(target[0], target[1], defaultLocationCircleRadius, locationModel.getLocalPaint());
//            }
//
            for (Map.Entry<String, PointF> entry : positions.entrySet()) {
                float[] target = {positionColor.get(entry.getKey()).getLocation().x - svgMapView.getFloorFeature().getOffset_x(),
                        positionColor.get(entry.getKey()).getLocation().y - svgMapView.getFloorFeature().getOffset_y()};
                matrix.mapPoints(target);
                canvas.drawCircle(target[0], target[1],
                        defaultLocationCircleRadius, positionColor.get(entry.getKey()).getLocalPaint());
            }
        }
        canvas.restore();
        Log.d(TAG, "draw: 执行完成了");
    }
}

